#!/bin/bash
#
# Implementation of the two-file subscription mechanism for Pulley
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 18 November 2013
#
#   Copyright 2012-2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## psub_twofile_extract_signature <subscription file> <output file>
##
## Extracts the payload signature from the <subscription file>, saving it
## to the <output file>. Returns 0 on success, non-zero if an error occurs.
##
function psub_twofile_extract_signature() {
	divide_file "$1" "__SIGNATURE_FOLLOWS__" > "${signature}"
	status=$?
}


## psub_twofile_download_payload <subscription file> <output file>
##
## Downloads the Pulley payload file specified in the <subscription file>,
## saving the payload in <output file>. Returns 0 on success, non-zero
## otherwise.
##
function psub_twofile_download_payload() {
	local payload_url=$(file_value "$1" "PAYLOAD_URL")
	local compression=$(file_value "$1" "PAYLOAD_COMPRESSION")
	local status=0
	
	[ -z "${compression}" ] && compression="none"
	
	if pcomp_supported "${compression}"; then
		if [[ -n "${payload_url}" ]]; then
			p_download "${payload_url}" "$2.in"
			status=$?
		
			if ((status == 0)); then
				pcomp_decompress "${compression}" "$2.in" "$2"
				status=$?
			fi
		else
			status=9
		fi
	else
		echo "ERROR: Compression format ${compression} is unavailable"
		status=109
	fi
	
	return ${status}
}


## psub_twofile_check_update <subscription file>
##
## Checks to see if a subscription update is available by comparing the
## current local copy of the signature file with the signature stored in the
## <subscription file>. Returns 0 if no update is available, 100 if an update
## is available, or other non-zero value if an error occurs.
##
function psub_twofile_check_update() {
	local signature=
	local sigfile=
	local status=$?
	
	if ((status == 0)); then
		signature=$(s_mktempfile)
		psub_twofile_extract_signature "$1" "${signature}"
		status=$?
		
		if ((status == 0)); then
			psub_diff_signature "${signature}"
			status=$?
			
			if ((status != 0)); then
				status=100
			fi
		fi
	fi
	
	return ${status}
}


## psub_twofile_update_subscription <output directory> <subscription file>
##
## Downloads the subscription payload file, using the <subscription file>
## to locate the payload URL. Extracts the signature file from the
## <subscription file>. Stores the resulting payload and signature files
## in the <output directory>. Returns 0 on success, non-zero if a failure
## occurs.
##
function psub_twofile_update_subscription() {
	local status=0
	
	psub_twofile_download_payload "$2" "$1/payload"
	status=$?
	
	if ((status == 0)); then
		psub_twofile_extract_signature "$2" "$1/signature"
		status=$?
	fi
	
	return ${status}
}
